package leetCode;

public class t0207 {
    public static void main(String[] args) {
        ListNode nodeA=new ListNode(1,new ListNode(2,new ListNode(6,new ListNode(3,new ListNode(4,new ListNode(5,new ListNode(6,null)))))));
        ListNode nodeB=new ListNode(2,new ListNode(3,new ListNode(6,new ListNode(3,new ListNode(4,new ListNode(5,new ListNode(6,null)))))));
        new Solution0207().getIntersectionNode(nodeA,nodeB);
    }
}
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }determined-i3uck
 */
class Solution0207 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode dummyNodeA=new ListNode(0,headA);
        ListNode dummyNodeB=new ListNode(0,headB);
        ListNode curA=dummyNodeA;
        ListNode curB=dummyNodeB;
        int lengthA=0;
        int lengthB=0;
        while (curA.next!=null){
            lengthA++;
            curA=curA.next;

        }
        while (curB.next!=null){
            lengthB++;
            curB=curB.next;
        }
        curA=dummyNodeA;
        curB=dummyNodeB;
        int duration=lengthA-lengthB;
        if (duration>0){
            while (duration-->0){
                curA=curA.next;
            }
        }else {
            duration=Math.abs(duration);
            while (duration-->0){
                curB=curB.next;
            }
        }
        while (curB!=null&&curA!=null&&curB.next!=curA.next){
            curA=curA.next;
            curB=curB.next;
        }
        if (curB==null||curA==null){
            return null;
        }
        return curA.next;
    }
}